import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:reader_app/page_card/base_card.dart';
import 'package:reader_app/page_card/web_page.dart';
import 'package:reader_app/test_page/test_page.dart';
import 'package:reader_app/utils/permission_helper.dart';

const NEED_PERMISSIONS = [PermissionGroup.storage, PermissionGroup.location, PermissionGroup.phone];

class CardTest extends BaseCard {
  @override
  _CardTestState createState() => _CardTestState();
}

class _CardTestState extends BaseCardState {
  @override
  Widget subTitle(String title) {
    var subTitle = super.subTitle("测试webView");

    return new InkWell(
      child: subTitle,
      onTap: () {
        Navigator.of(context).push(new MaterialPageRoute(builder: (con) {
          return WebPage();
        }));
      },
    );
  }

  @override
  Widget bottomTitle(String title) {
    return new InkWell(
      child: super.bottomTitle("去预研测试页面"),
      onTap: () {
        Navigator.of(context).push(new MaterialPageRoute(builder: (con) {
          return TestPage();
        }));
      },
    );
  }

  @override
  Widget smallTopTitle(String smallTopTitle) {
    return new InkWell(
      child: super.smallTopTitle("去预研测试页面"),
      onTap: () {
        Navigator.of(context).push(new MaterialPageRoute(builder: (con) {
          return TestPage();
        }));
      },
    );
  }

  @override
  Widget topTitle(String topTitle) {
    return super.topTitle("练练才知道");
  }

  @override
  Widget bottomContent() {
    return new Expanded(
        child: new Column(
      children: <Widget>[
        new InkWell(
          child: new Text("把图标写到本地存储里"),
          onTap: () {
            PermissionHelper.instance().requestPermission(NEED_PERMISSIONS);
          },
        ),
        new FutureBuilder<bool>(
          initialData: false,
          future: PermissionHelper.instance().checkPermissionsHasGranted(NEED_PERMISSIONS),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              if (snapshot.data == true) {
                return new InkWell(
                  onTap: () {
                    PermissionHelper.instance().requestPermission(NEED_PERMISSIONS);
                  },
                  child: new Text("已经全部获取到所需权限"),
                );
              } else {
                return new InkWell(
                  onTap: () {
                    PermissionHelper.instance().shouldShowRequestPermissionRationale(NEED_PERMISSIONS).then((isShow) {
                      if (isShow) {
                        PermissionHelper.instance().requestPermission(NEED_PERMISSIONS);
                      } else {
                        PermissionHelper.instance().openSetting();
                      }
                    });
                  },
                  child: new Text("永远不提示了"),
                );
              }
            }
            return new RefreshProgressIndicator(semanticsLabel: "等待中");
          },
        ),
        new SizedBox(
          height: 10,
        ),
        new InkWell(
          child: new Text("https://www.jianshu.com/p/f0851290aa5b"),
          onTap: () {
            Navigator.of(context).push(new MaterialPageRoute(builder: (con) {
              return WebPage(
                url: "https://www.jianshu.com/p/f0851290aa5b",
              );
            }));
          },
        ),
        new SizedBox(
          height: 10,
        ),
        new InkWell(
          child: new Text("http://baijiahao.baidu.com/s?id=1662109442980382790&title=%7B%22ds%22%3A%221%22%7D"),
          onTap: () {
            Navigator.of(context).push(new MaterialPageRoute(builder: (con) {
              return WebPage(
                url: "http://baijiahao.baidu.com/s?id=1662109442980382790&title=%7B%22ds%22%3A%221%22%7D",
              );
            }));
          },
        ),
        new SizedBox(
          height: 10,
        ),
        new InkWell(
          child: new Text("http://baijiahao.baidu.com/s?id=1662109442980382790&title=%e5%93%88"),
          onTap: () {
            Navigator.of(context).push(new MaterialPageRoute(builder: (con) {
              return WebPage(
                url: "http://baijiahao.baidu.com/s?id=1662109442980382790&title=%e5%93%88",
              );
            }));
          },
        )
      ],
    ));
  }
}
